package redis

import (
	"fmt"
	"github.com/go-redis/redis"
	"github.com/rs/zerolog/log"
	"time"
)

func set(key, value string, expiration int) {
	// TODO 这儿的时间要处理一下
	err := rdb.Set(key, value, time.Duration(expiration)).Err()
	if err != nil {
		log.Error().Msgf("redis数据库set操作出错:%v", err)
	}
}

func get(key string) string {
	value, err := rdb.Get(key).Result()
	if err != nil {
		log.Error().Msgf("redis数据库get操作出错:%v", err)
	}
	return value
}

func ttl(key string) time.Duration {
	duration, err := rdb.TTL(key).Result()
	if err != nil {
		log.Error().Msgf("redis数据库ttl操作出错:%v", err)
	}
	return duration
}

func rpush(key, value string) {
	err := rdb.RPush(key, value).Err()
	if err != nil {
		log.Error().Msgf("redis数据库rpush操作出错:%v", err)
	}
}

func llen(key string) int64 {
	list_len, err := rdb.LLen(key).Result()
	if err != nil {
		log.Error().Msgf("redis数据库LLen操作出错:%v", err)
	}
	return list_len
}

func blpop(key string, timeout time.Duration) []string {
	result, err := rdb.BLPop(timeout, key).Result()
	if err != nil {
		log.Error().Msgf("redis数据库blpop操作出错:%v", err)
	}
	return result
}

func hset(key, field, value string, expiration int) {
	err := rdb.HSet(key, field, value).Err()
	if err != nil {
		log.Error().Msgf("redis数据库hset操作出错:%v", err)
	}
}

func err_handler(err error) {
	fmt.Printf("err_handler, error:%s\n", err.Error())
	panic(err.Error())
}

func standalon_redis_test() {

	client := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "",
		DB:       0,
	})

	defer client.Close()

	kv_map := make(map[string]interface{})
	kv_map["3"] = "f3"
	kv_map["4"] = "f4"

	err := client.HMSet("tmap", kv_map).Err()
	if err != nil {
		fmt.Printf("try mset map[tmap] field[3] field[4] error[%s]\n",
			err.Error())
		err_handler(err)
	}

	map_len, err := client.HLen("tmap").Result()
	if err != nil {
		fmt.Printf("try get len of map[tmap] error[%s]\n", err.Error())
		err_handler(err)
	}
	fmt.Printf("len of map[tmap] is %d\n", map_len)

	// get map value
	value, err := client.HGet("tmap", "1").Result()
	if err != nil {
		fmt.Printf("try get field[1] value of map[tmap] error[%s]\n",
			err.Error())
		err_handler(err)
	}

	fmt.Printf("field[1] value of map[tmap] is %s\n", value)

	// hgetall
	result_kv, err := client.HGetAll("tmap").Result()
	if err != nil {
		fmt.Printf("try hgetall map[tmap] error[%s]\n", err.Error())
		err_handler(err)
	}

	for k, v := range result_kv {
		fmt.Printf("map[tmap] %s = %s\n", k, v)
	}

	fmt.Printf("----------------------------------------\n")

	fmt.Printf("pubsub test\n")

	pubsub := client.Subscribe("test_channel")

	_, err = pubsub.Receive()
	if err != nil {
		fmt.Printf("try subscribe channel[test_channel] error[%s]\n",
			err.Error())
		err_handler(err)
	}

	// go channel to used to receive message
	ch := pubsub.Channel()

	// publish a message
	err = client.Publish("test_channel", "hello").Err()
	if err != nil {
		fmt.Printf("try publish message to channel[test_channel] error[%s]\n",
			err.Error())
		err_handler(err)
	}

	time.AfterFunc(time.Second*2, func() {
		_ = pubsub.Close()
	})

	// consume message
	for {
		msg, ok := <-ch
		if !ok {
			break
		}

		fmt.Printf("recv message[%s] from channel[%s]\n",
			msg.Payload, msg.Channel)
	}
}
